import createUtil from '../../libs/util.js';

const util = createUtil();

const state = {
    show: {
        menu: false,
        normal: false, scan: false
    },
    status: {
        predictPrice: false,
        allSelect: false
    },
    mode: 'all', // all, normal, scan
    selectMode: 'normal', // normal | scan
    operateMode: 'will', // will, stock
    order: '',
    orderPrice: 0,
    carts: [],
    selectItems: [],
    stock: {
        select: [],
        allSelect: false
    }
};

const getters = {};

const mutations = {
    setElementValue(state, params) {
        // console.log(params);

        if (params instanceof Array) {
            for (let x of params) {
                let {key, value} = x,
                    k = key.split('.');

                switch (k.length) {
                    case 2:
                        state[k[0]][k[1]] = value;
                        break;
                    case 3:
                        state[k[0]][k[1]][k[2]] = value;
                        break;
                    default:
                        state[k[0]] = value;
                        break;
                }
            }
        } else {
            let {key, value} = params;
            state[key] = value;
        }
    },
    addShopCartsItem(state, params) {
        state.carts.push(params);
        // console.log('state.selectItems ',state.selectItems)
        state.selectItems.push(state.carts.length - 1);
        // console.log(' .selectItems ',state.selectItems)
    },
    deleteShopCartsItem(state, index) {
        state.selectItems = state.selectItems.filter(val => val != index)
        state.carts.splice(index, 1);
        console.log('state.carts ', state.carts)
        console.log('state ', state.selectItems)
        // state.selectItems.splice(index, 1);
        // state.selectItems.push(state.carts.length - 2);
    },
    addSelectItem(state, index) {
        state.selectItems.push(index);
    },
    allSelectCartsItem(state, items) {
        state.selectItems = items;
        // state.selectItems = state.selectItems.filter(val => val != index)
    },
    deleteSelectItem(state, index) {
        state.selectItems = state.selectItems.filter(val => val != index)
      /*  for (let i in state.selectItems) {
            if (state.selectItems[i] == index) {
                state.selectItems.splice(i, 1);
                break;
            }
        }*/
    },
    resetShopCartsDataset(state, params) {
        state.carts = params;
    },
    stockSelectItemOperate(state, params) {
        console.log(params);

        switch (params.mode) {
            case 'select':
                state.stock.select.push(params.index);
                break;
            case 'unSelect':
                for (let i in state.stock.select) {
                    if (params.index == state.stock.select[i]) {
                        state.stock.select.splice(i, 1);
                    }
                }
                break;
        }
    },
    settingShopMode(state, params) {
        // console.log('set-shop-mode', params);

        state.mode = params;
    }
};

const actions = {};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
};
